package csc309.datingapp;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
    
/*   
 * This interceptor provides authentication for the secure actions of the application.
 * It checks the session scope map to see if there's user 
 * object present, which indicates that the current user is already logged in.  If this
 * object is not present, the interceptor alters the workflow of the request by returning 
 * a login control string that causes the request to forward to the login page.
 * 
 */   

public class AuthenticationInterceptor implements Interceptor {

	public void destroy() {
	}

	public void init() {
	}  

	public String intercept( ActionInvocation actionInvocation ) throws Exception {

		Map session = actionInvocation.getInvocationContext().getSession();
		
		
		String username = (String) session.get( "Username" );
		
		/* 
		 * If username doesn't exist return the LOGIN control string.  This will cause the 
		 * execution of this action to stop and the request will return the globally defined
		 * login result.
		 */
		if (username == null) {
		    return Action.LOGIN;
		} 
		
		/*
		 * Otherwise, continue the execution of this request by invoking the rest of the   
		 * interceptor stack, and ultimately, the action. 
		 */
		else {
		    System.out.println("Logged in: interceptor");
		    return actionInvocation.invoke();
		}

	}
	
	
}
